package NIO;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @author 47868
 */
public class 阻塞式网络通信客户端 {
    public static void main(String[] args)throws Exception{
        Scanner scanner = new Scanner(System.in);

        SocketChannel channel=SocketChannel.open();
        channel.connect(new InetSocketAddress("localhost",8080));
        ByteBuffer buffer=ByteBuffer.allocate(16);
        while(true){
            String message = scanner.nextLine();
            if("exit".equals(message)){
                channel.close();
                break;
            }
            buffer.put(message.getBytes(StandardCharsets.UTF_8));
            buffer.flip();
            channel.write(buffer);
            buffer.clear();
        }
        /*System.out.println("waiting...");*/
    }
}
